package com.account.book.service.impl;

import com.account.book.entity.ApprovalInfo;
import com.account.book.entity.Book;
import com.account.book.entity.User;
import com.account.book.mapper.ApprovalInfoMapper;
import com.account.book.request.UserApplyBookReqVo;
import com.account.book.response.ApprovalInfoResVo;
import com.account.book.service.ApprovalInfoService;
import com.account.book.service.BookService;
import com.account.book.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ma.glasnost.orika.MapperFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liang-hw
 * @className ApprovalInfoServiceImpl
 * @description 审批记录信息
 * @date 2024/01/19 17:02:24
 */
@Service
public class ApprovalInfoServiceImpl extends ServiceImpl<ApprovalInfoMapper, ApprovalInfo> implements ApprovalInfoService {
    private static final Logger logger = LoggerFactory.getLogger(ApprovalInfoServiceImpl.class);
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;
    @Autowired
    private MapperFacade mapperFacade;

    /**
     * @param @param userApplyBookReqVo
     * @param userId
     * @return
     * @author liang-hw
     * @description 新增审批记录信息
     * @date 2024/01/19 17:23:05
     */
    @Override
    public void addApprovalInfo(UserApplyBookReqVo userApplyBookReqVo, String userId, Integer status) {
        ApprovalInfo approvalInfo = new ApprovalInfo();
        approvalInfo.setBookId(userApplyBookReqVo.getBookId());
        approvalInfo.setApprovalUserId(userApplyBookReqVo.getUserId());
        approvalInfo.setApprovalStatus(status);
        approvalInfo.setUserId(userId);
        baseMapper.insert(approvalInfo);
    }

    /**
     * @param @param userId
     * @return @return {@link List }<{@link ApprovalInfoResVo }>
     * @author liang-hw
     * @description 查询审批信息
     * @date 2024/01/20 16:57:12
     */
    @Override
    public List<ApprovalInfoResVo> selectApprovalInfo(String userId) {
        List<ApprovalInfoResVo> approvalInfoResVoList = new ArrayList<>();
        QueryWrapper<ApprovalInfo> approvalInfoQueryWrapper = new QueryWrapper<>();
        approvalInfoQueryWrapper.lambda().eq(ApprovalInfo::getUserId, userId)
                .orderByDesc(ApprovalInfo::getCreateTime);
        List<ApprovalInfo> approvalInfos = baseMapper.selectList(approvalInfoQueryWrapper);
        List<ApprovalInfoResVo> approvalInfoResVos = mapperFacade.mapAsList(approvalInfos, ApprovalInfoResVo.class);
        for (ApprovalInfoResVo approvalInfoResVo : approvalInfoResVos) {
            Book book = bookService.getById(approvalInfoResVo.getBookId());
            User approvalUser = userService.getById(approvalInfoResVo.getApprovalUserId());
            User user = userService.getById(approvalInfoResVo.getUserId());
            approvalInfoResVo.setBookName(book.getBookName());
            approvalInfoResVo.setApprovalUserName(approvalUser.getNickName());
            approvalInfoResVo.setUserName(user.getNickName());
            approvalInfoResVoList.add(approvalInfoResVo);
        }
        return approvalInfoResVoList;
    }
}
